//
//  WCEditDefinesSheetController.m
//  WabbitCode
//
//  Created by William Towe on 4/10/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCEditDefinesSheetController.h"
#import "WCBuildConfiguration.h"
#import "WCBuildTargetGetInfoWindowController.h"
#import "WCBuildDefine.h"


@implementation WCEditDefinesSheetController
- (id)init {
	if (!(self = [super initWithWindowNibName:@"WCEditDefinesSheet"]))
		return nil;
	
	_defines = [[NSMutableArray alloc] init];
	_newDefineCounter = 0;
	
	return self;
}

- (void)dealloc {
	[_defines release];
	[super dealloc];
}

+ (void)presentEditDefinesSheetForBuildConfiguration:(WCBuildConfiguration *)config forGetInfoWindowController:(WCBuildTargetGetInfoWindowController *)getInfoWindowController; {
	WCEditDefinesSheetController *controller = [[[self class] alloc] init];
	
	[controller setBuildConfiguration:config];
	[controller setGetInfoWindowController:getInfoWindowController];
	
	for (WCBuildDefine *define in [config defines]) {
		[[controller defines] addObject:[[define copy] autorelease]];
	}
	
	[NSApp beginSheet:[controller window] modalForWindow:[getInfoWindowController window] modalDelegate:controller didEndSelector:@selector(_sheetDidEnd:code:info:) contextInfo:NULL];
}

@synthesize buildConfiguration=_buildConfiguration,getInfoWindowController=_getInfoWindowController,defines=_defines;

- (IBAction)addDefineAction:(id)sender; {
	WCBuildDefine *define = [WCBuildDefine buildDefineWithName:[NSString stringWithFormat:NSLocalizedString(@"NEW_DEFINE%u",@"new define name"), ++_newDefineCounter]];
	
	[[_tableView dataSource] addObject:define];
	[_tableView editColumn:[_tableView columnWithIdentifier:@"Name"] row:[_tableView selectedRow] withEvent:nil select:YES];
}

- (void)_sheetDidEnd:(NSWindow *)window code:(NSInteger)code info:(void *)info {
	[self autorelease];
	if (code != NSOKButton)
		return;
	
	[[self buildConfiguration] setDefines:[self defines]];
}
@end
